﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using System.Collections.Generic;

public partial class System_CodeItemNew : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCodeGroup();
            BindParentList("");
            BindSeqList();

            if (Request.QueryString["id"] == null)
            {
                ActionTitle.Text = "添加编码";
            }
            else
            {
                ActionTitle.Text = "编辑编码";
                BindData();
                ItemID.ReadOnly = true;
            }
        }
    }

    void BindCodeGroup()
    {
        CodeGroup.DataSource = Yipusoft.Common.BLL.CodeGroup.GetList("");
        CodeGroup.DataBind();
    }

    private void BindParentList(string parentID)
    {

        ListItem li = new ListItem();
        if (parentID == "")
        {
            li.Text = "顶级编码";
            li.Value = "";
            ParentList.Items.Add(li);
        }
        List<CodeItemInfo> items = CodeItem.GetList2("ParentID='" + parentID + "' and GroupID='" + CodeGroup.SelectedValue + "'  order by ItemID ");
        foreach (CodeItemInfo item in items)
        {
            li = new ListItem();
            li.Text = "".PadRight(2 * item.ItemLevel, '　') + item.ItemName + "(" + item.ItemID + ")";
            li.Value = item.ItemID;
            ParentList.Items.Add(li);
            BindParentList(item.ItemID);
        }
    }
    private void BindSeqList()
    {
        ListItem li;
        for (int i = 0; i < 50; i++)
        {
            li = new ListItem();
            li.Text = i.ToString();
            li.Value = i.ToString();
            SeqList.Items.Add(li);
        }
    }
    private void BindData()
    {
        CodeItemInfo item = CodeItem.GetCodeItem(new Guid(Request.QueryString["id"]));
        ItemID.Text = item.ItemID;
        ItemName.Text = item.ItemName;
        CodeGroup.SelectedValue = item.GroupID;
        ParentList.Items.Clear();
        BindParentList("");
        ParentList.SelectedValue = item.ParentID;
        SeqList.SelectedValue = item.SeqID.ToString();
        Description.Text = item.Description;

    }
    private void Insert()
    {
        if (CodeItem.Exsits(ItemID.Text, CodeGroup.SelectedValue))
        {
            Prompt.Text = "警告：该编码已存在！";
            return;
        }
        CodeItemInfo item = new CodeItemInfo();
        item.ItemID = ItemID.Text;
        item.ItemName = ItemName.Text;
        item.ParentID = ParentList.SelectedValue;
        item.GroupID = CodeGroup.SelectedValue;
        if (ParentList.SelectedValue != "")
            item.ItemLevel = CodeItem.GetCodeItem(ParentList.SelectedValue, CodeGroup.SelectedValue).ItemLevel + 1;

        else
            item.ItemLevel = 0;
        item.SeqID = int.Parse(SeqList.SelectedValue);
        item.Description = Description.Text;
        CodeItem.Insert(item);

        EventLogInfo log = new EventLogInfo();
        log.EventTime = DateTime.Now;
        log.EventType = 1;
        log.EventData = "添加了一条编码";
        log.ItemID = ItemID.Text;
        log.ItemName = "CodeItems";
        log.ItemFullUrl = "CodeItemNew.aspx";
        log.CreatedBy = Context.User.Identity.Name;
        log.CreatedTime = DateTime.Now;
        log.UserIP = Request.UserHostAddress;
        EventLog.Insert(log);

        Prompt.Text = "提示：添加成功！";
    }
    private void Update()
    {
        CodeItemInfo item = CodeItem.GetCodeItem(new Guid(Request.QueryString["id"]));
        item.ItemName = ItemName.Text;
        item.ParentID = ParentList.SelectedValue;
        if (ParentList.SelectedValue != "")
            item.ItemLevel = CodeItem.GetCodeItem(ParentList.SelectedValue, CodeGroup.SelectedValue).ItemLevel + 1;
        else
            item.ItemLevel = 0;
        item.SeqID = int.Parse(SeqList.SelectedValue);
        item.Description = Description.Text;
        item.GroupID = CodeGroup.SelectedValue;
        CodeItem.Update(item);

        EventLogInfo log = new EventLogInfo();
        log.EventTime = DateTime.Now;
        log.EventType = 1;
        log.EventData = "更新了一条编码";
        log.ItemID = ItemID.Text;
        log.ItemName = "CodeItems";
        log.ItemFullUrl = "CodeItemNew.aspx";
        log.CreatedBy = Context.User.Identity.Name;
        log.CreatedTime = DateTime.Now;
        log.UserIP = Request.UserHostAddress;
        EventLog.Insert(log);

        Prompt.Text = "提示：更新成功！";
    }
    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] == null)
            Insert();
        else
            Update();
    }
    protected void CodeGroup_SelectedIndexChanged(object sender, EventArgs e)
    {
        ParentList.Items.Clear();
        BindParentList("");
    }
}
